//
//  NCChatViewLeftPhotoCell.m
//  AGEmojiKeyboard
//
//  Created by LINAICAI on 2018/4/17.
//

#import "NCChatViewLeftPhotoCell.h"

@implementation NCChatViewLeftPhotoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.usrNameLabel.font = [UIFont systemFontOfSize:13];
    self.usrNameLabel.textColor = [UIColor grayColor];

    self.avatorImage.layer.cornerRadius = 34.0/2;
    self.avatorImage.layer.masksToBounds = YES;
    

    self.contentImage.layer.borderWidth = 4.0;
    self.contentImage.layer.cornerRadius = 4.0;
    self.contentImage.layer.masksToBounds = YES;
    self.contentImage.layer.borderColor = [UIColor colorWithRed:241/255.0 green:240/255.0 blue:240/255.0 alpha:1.0].CGColor;
    self.contentImage.backgroundColor = [UIColor colorWithRed:241/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    self.contentImage.contentMode = UIViewContentModeScaleToFill;
    [self.contentImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTap)]];
    self.contentImage.userInteractionEnabled = YES;
    
    ///监听message的缩略图状态
    [self addObserver:self forKeyPath:@"message.previewImage" options: NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"message.previewImage"]) {
        //拿到新值/旧值,进行操作
        NSUInteger new = change[@"new"];
        NSUInteger old = change[@"old"];
        if (new == old) {
            
        }
        else{
            [self setupImage];
        }
    }
    else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"message.previewImage"];
}
- (void)setupImage{
    self.contentImage.image = self.message.previewImage;
}
- (void)setMessage:(NCChatPhotoMessage *)message{
    [super setMessage:message];
    self.contentImage.image = message.previewImage;
    
    //根据图片大小调整气泡的宽度
    [self.contentImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(message.previewSize);
        
    }];
}
- (void)imageTap{
    //回调出去
    NSDictionary *userInfo = @{@"message":self.message};
    [[NSNotificationCenter defaultCenter]postNotificationName:NCChatMessagePhotoTapNotifi object:nil userInfo:userInfo];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
